//
//  OverViewMapMenu.m
//  cocoapodText
//
//  Created by apple on 16/9/16.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ShiPei.h"
#import "OverViewMapMenu.h"
#import <Foundation/Foundation.h>

@interface OverViewMapMenu()
@end

@implementation OverViewMapMenu

/**
 *  初始化视图属性
 *
 *  @param frame 视图frame
 *
 *  @return self
 */
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //定制View
        self.backgroundColor = [UIColor clearColor];
        self.userInteractionEnabled = YES; //设置为NO后，不再响应touch方法
        self.multipleTouchEnabled = YES;
        //控制子视图不能超出父视图的范围
        self.clipsToBounds = YES;
        //添加子视图
        [self subView];
    }
    return self;
}

//子视图懒加载
-(void)subView{
    //背景
    UIImageView *GYMenuBGImage=[[UIImageView alloc]initWithFrame:[ShiPei CGRectMakeScale:0 :0 :145 :410]];
    [GYMenuBGImage setUserInteractionEnabled:YES];
    [GYMenuBGImage setBackgroundColor:[UIColor clearColor]];
    [self addSubview:GYMenuBGImage];
    //半透明
    UIImageView *GYMenuTranslucentBGImage=[[UIImageView alloc]initWithFrame:[ShiPei CGRectMakeScale:80 :0 :65 :410]];
    [self addSubview:GYMenuTranslucentBGImage];
    [GYMenuTranslucentBGImage setUserInteractionEnabled:YES];
    [GYMenuTranslucentBGImage setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.1]];
    //定位
    UIView *MapMenuLoc=[[UIView alloc]initWithFrame:[ShiPei CGRectMakeScale:0 :20 :130 :35]];
    [self addSubview:MapMenuLoc];
    [MapMenuLoc setBackgroundColor:FUIColorFromRGB(0x58afd7)];
    [MapMenuLoc setUserInteractionEnabled:YES];
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickLocMenu)];
    [MapMenuLoc addGestureRecognizer:singleTap];
    self.ItemAreaView=MapMenuLoc;
    
    UIImageView *MapMenuLocImage=[[UIImageView alloc]initWithFrame:[ShiPei CGRectMakeScale:0 :0 :35 :35]];
    [MapMenuLoc addSubview:MapMenuLocImage];
    [MapMenuLocImage setUserInteractionEnabled:YES];
    [MapMenuLocImage setImage:[UIImage imageNamed:@"info_map_loc.png"]];
    
    UILabel *MapMenuLocLabel=[[UILabel alloc]initWithFrame:[ShiPei CGRectMakeScale:35 :0 :95 :35]];
    [MapMenuLoc addSubview:MapMenuLocLabel];
    [MapMenuLocLabel setUserInteractionEnabled:YES];
    [MapMenuLocLabel setText:@"贵阳市"];
    [MapMenuLocLabel setTextAlignment:NSTextAlignmentCenter];
    [MapMenuLocLabel setTextColor:[UIColor whiteColor]];
    [MapMenuLocLabel setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:30]]];
    self.ItemAreaLabel=MapMenuLocLabel;
    
    //饼1
    [self addImageMenuItemWithTag:1 image:@"info_map_01.png" frame:[ShiPei CGRectMakeScale:95 :65 :35 :35] text01:@"企业" text02:@"数量"];
    
    //柱2
    [self addImageMenuItemWithTag:2 image:@"info_map_02.png" frame:[ShiPei CGRectMakeScale:95 :110 :35 :35]text01:@"昨日" text02:@"能耗"];
    
    //电3
    [self addImageMenuItemWithTag:3 image:@"info_map_03.png" frame:[ShiPei CGRectMakeScale:95 :155 :35 :35]text01:@"昨日" text02:@"用电"];
    
    //水4
    [self addImageMenuItemWithTag:4 image:@"info_map_04.png" frame:[ShiPei CGRectMakeScale:95 :200 :35 :35]text01:@"昨日" text02:@"用水"];
    
    //暖5
    [self addImageMenuItemWithTag:5 image:@"info_map_05.png" frame:[ShiPei CGRectMakeScale:95 :245 :35 :35]text01:@"昨日" text02:@"用气"];
    
    //冷6
    [self addImageMenuItemWithTag:6 image:@"info_map_06.png" frame:[ShiPei CGRectMakeScale:95 :290 :35 :35]text01:@"昨日" text02:@"用冷"];
}

-(void)addImageMenuItemWithTag:(int)tag image:(NSString *)image frame:(CGRect )frame text01:(NSString *)text01 text02:(NSString *)text02{
    
    UIView *MapMenuItemBackView=[[UIView alloc]initWithFrame:frame];
    [self addSubview:MapMenuItemBackView];
    [MapMenuItemBackView setTag:tag];
    [MapMenuItemBackView setBackgroundColor:FUIColorFromRGB(0x58afd7)];
    [MapMenuItemBackView setUserInteractionEnabled:YES];
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickMapMenuItem:)];
    [MapMenuItemBackView addGestureRecognizer:singleTap];
    
    UILabel *MapMenuItemLabel=[[UILabel alloc]initWithFrame:[ShiPei CGRectMakeScale:35 :0 :95 :35]];
    [MapMenuItemBackView addSubview:MapMenuItemLabel];
    [MapMenuItemLabel setHidden:YES];
    [MapMenuItemLabel setUserInteractionEnabled:YES];
    [MapMenuItemLabel setText:@""];
    [MapMenuItemLabel setTextAlignment:NSTextAlignmentCenter];
    [MapMenuItemLabel setTextColor:[UIColor whiteColor]];
    [MapMenuItemLabel setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:25]]];
    
    UIImageView *MapMenuItemImage=[[UIImageView alloc]initWithFrame:[ShiPei CGRectMakeScale:0 :0 :35 :35]];
    [MapMenuItemBackView addSubview:MapMenuItemImage];
    [MapMenuItemImage setUserInteractionEnabled:YES];
    [MapMenuItemImage setImage:[UIImage imageNamed:image]];
    
    UIView *MapMenuItemDesc=[[UIView alloc]initWithFrame:[ShiPei CGRectMakeScale:0 :0 :35 :35]];
    [MapMenuItemBackView addSubview:MapMenuItemDesc];
    [MapMenuItemDesc setBackgroundColor:FUIColorFromRGB(0x58afd7)];
    [MapMenuItemDesc setHidden:YES];
    
    UILabel *MapMenuItemLabelDesc01=[[UILabel alloc]initWithFrame:[ShiPei CGRectMakeScale:0 :3 :35 :16]];
    [MapMenuItemDesc addSubview:MapMenuItemLabelDesc01];
    [MapMenuItemLabelDesc01 setUserInteractionEnabled:YES];
    [MapMenuItemLabelDesc01 setText:text01];
    [MapMenuItemLabelDesc01 setTextAlignment:NSTextAlignmentCenter];
    [MapMenuItemLabelDesc01 setTextColor:[UIColor whiteColor]];
    [MapMenuItemLabelDesc01 setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:18]]];
    
    UILabel *MapMenuItemLabelDesc02=[[UILabel alloc]initWithFrame:[ShiPei CGRectMakeScale:0 :35/2 :35 :16]];
    [MapMenuItemDesc addSubview:MapMenuItemLabelDesc02];
    [MapMenuItemLabelDesc02 setUserInteractionEnabled:YES];
    [MapMenuItemLabelDesc02 setText:text02];
    [MapMenuItemLabelDesc02 setTextAlignment:NSTextAlignmentCenter];
    [MapMenuItemLabelDesc02 setTextColor:[UIColor whiteColor]];
    [MapMenuItemLabelDesc02 setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:18]]];
    
    switch (tag) {
        case 1:
            self.Item01=MapMenuItemBackView;
            self.ItemLabel01=MapMenuItemLabel;
            self.ItemDesc01=MapMenuItemDesc;
            break;
        case 2:
            self.Item02=MapMenuItemBackView;
            self.ItemLabel02=MapMenuItemLabel;
            self.ItemDesc02=MapMenuItemDesc;
            break;
        case 3:
            self.Item03=MapMenuItemBackView;
            self.ItemLabel03=MapMenuItemLabel;
            self.ItemDesc03=MapMenuItemDesc;
            break;
        case 4:
            self.Item04=MapMenuItemBackView;
            self.ItemLabel04=MapMenuItemLabel;
            self.ItemDesc04=MapMenuItemDesc;
            break;
        case 5:
            self.Item05=MapMenuItemBackView;
            self.ItemLabel05=MapMenuItemLabel;
            self.ItemDesc05=MapMenuItemDesc;
            break;
        case 6:
            self.Item06=MapMenuItemBackView;
            self.ItemLabel06=MapMenuItemLabel;
            self.ItemDesc06=MapMenuItemDesc;
            break;
        default:
            break;
    }
}

-(void)clickMapMenuItem:(UITapGestureRecognizer *)gestureRecognizer{
    NSInteger tag=gestureRecognizer.view.tag;
    [self clickMapMenuWithTab:[[NSString stringWithFormat:@"%ld",(long)tag]intValue]];
}


-(void)OpenItemWithTab:(UIView *)tab{
    CGRect frame=tab.frame;
    if (frame.origin.x==0) {
        [self CloseItemWithTab:tab];
    }else{
        frame.origin.x=0;
        frame.size.width=130;
        [tab setFrame:frame];
        switch(tab.tag){
            case 1:
                [self.ItemLabel01 setHidden:NO];
                [self.ItemDesc01 setHidden:NO];
                break;
            case 2:
                [self.ItemLabel02 setHidden:NO];
                [self.ItemDesc02 setHidden:NO];
                break;
                
            case 3:
                [self.ItemLabel03 setHidden:NO];
                [self.ItemDesc03 setHidden:NO];
                break;
                
            case 4:
                [self.ItemLabel04 setHidden:NO];
                [self.ItemDesc04 setHidden:NO];
                break;
                
            case 5:
                [self.ItemLabel05 setHidden:NO];
                [self.ItemDesc05 setHidden:NO];
                break;
                
            case 6:
                [self.ItemLabel06 setHidden:NO];
                [self.ItemDesc06 setHidden:NO];
                break;
        }
    }
    
}

-(void)CloseItemWithTab:(UIView *)tab{
    CGRect frame=tab.frame;
    frame.origin.x=95;
    frame.size.width=35;
    [tab setFrame:frame];
    switch(tab.tag){
        case 1:
            [self.ItemLabel01 setHidden:YES];
            [self.ItemDesc01 setHidden:YES];
            break;
        case 2:
            [self.ItemLabel02 setHidden:YES];
            [self.ItemDesc02 setHidden:YES];
            break;
            
        case 3:
            [self.ItemLabel03 setHidden:YES];
            [self.ItemDesc03 setHidden:YES];
            break;
            
        case 4:
            [self.ItemLabel04 setHidden:YES];
            [self.ItemDesc04 setHidden:YES];
            break;
            
        case 5:
            [self.ItemLabel05 setHidden:YES];
            [self.ItemDesc05 setHidden:YES];
            break;
        case 6:
            [self.ItemLabel06 setHidden:YES];
            [self.ItemDesc06 setHidden:YES];
            break;
    }
}

-(void)colseAllMenu{
    [self CloseItemWithTab:self.Item01];
    [self CloseItemWithTab:self.Item02];
    [self CloseItemWithTab:self.Item03];
    [self CloseItemWithTab:self.Item04];
    [self CloseItemWithTab:self.Item05];
    [self CloseItemWithTab:self.Item06];
}

-(void)clickMapMenuWithTab:(int)tab{
    if([_delegate respondsToSelector:@selector(onClickMapMenuWithTab:Menu:)]){
        [_delegate onClickMapMenuWithTab:tab Menu:self];
    }
    [self ResetMenuWithTab:tab];
}

-(void)ResetMenuWithTab:(int)tab{
    switch (tab) {
        case 1:
            [self OpenItemWithTab:self.Item01];
            [self CloseItemWithTab:self.Item02];
            [self CloseItemWithTab:self.Item03];
            [self CloseItemWithTab:self.Item04];
            [self CloseItemWithTab:self.Item05];
            [self CloseItemWithTab:self.Item06];
            break;
        case 2:
            [self CloseItemWithTab:self.Item01];
            [self OpenItemWithTab:self.Item02];
            [self CloseItemWithTab:self.Item03];
            [self CloseItemWithTab:self.Item04];
            [self CloseItemWithTab:self.Item05];
            [self CloseItemWithTab:self.Item06];
            break;
        case 3:
            [self CloseItemWithTab:self.Item01];
            [self CloseItemWithTab:self.Item02];
            [self OpenItemWithTab:self.Item03];
            [self CloseItemWithTab:self.Item04];
            [self CloseItemWithTab:self.Item05];
            [self CloseItemWithTab:self.Item06];
            break;
        case 4:
            [self CloseItemWithTab:self.Item01];
            [self CloseItemWithTab:self.Item02];
            [self CloseItemWithTab:self.Item03];
            [self OpenItemWithTab:self.Item04];
            [self CloseItemWithTab:self.Item05];
            [self CloseItemWithTab:self.Item06];
            break;
        case 5:
            [self CloseItemWithTab:self.Item01];
            [self CloseItemWithTab:self.Item02];
            [self CloseItemWithTab:self.Item03];
            [self CloseItemWithTab:self.Item04];
            [self OpenItemWithTab:self.Item05];
            [self CloseItemWithTab:self.Item06];
            break;
        case 6:
            [self CloseItemWithTab:self.Item01];
            [self CloseItemWithTab:self.Item02];
            [self CloseItemWithTab:self.Item03];
            [self CloseItemWithTab:self.Item04];
            [self CloseItemWithTab:self.Item05];
            [self OpenItemWithTab:self.Item06];
            break;
        default:
            break;
    }
}


-(void)clickLocMenu{
    if([_delegate respondsToSelector:@selector(onClickLocMenu:)]){
        [_delegate onClickLocMenu:self];
    }
    [self colseAllMenu];
}


@end
